//
//  AgingModel.m
//  Balance
//
//  Created by Calvin Kuo on 12-03-26.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AgingModel.h"

@implementation AgingModel

@synthesize delinquentAmounts = _delinquentAmounts;
@synthesize agingBucketLabels = _agingBucketLabels;


//???? should change to NSMutableDictionary and use the NSDate as key to get the amount
// or still use the array but populate with the same order in dailyKey array (ordered by date)
-(id)init
{
    if (self = [super init])
    { 
        //days in 2012, 2011, 2010, 2009, 2008
        numberOfDailyBalances = [self getNumberOfDaysThisYearTillToday] + 365 + 365 + 365 + 366;

        _delinquentAmounts = [[NSMutableArray alloc]init];
        
        
        for (int d=0; d < numberOfDailyBalances + 1; d++)
        {
            AgingBucket *agingBucketAmount = [[AgingBucket alloc] init];
            agingBucketAmount.bucketOne = [NSNumber numberWithInt:(2000+(d%10)*100)];
            agingBucketAmount.bucketTwo = [NSNumber numberWithInt:(1050+(d%10)*100)];
            agingBucketAmount.bucketThree = [NSNumber numberWithInt:(1500+(d%10)*100)];
            agingBucketAmount.bucketFour = [NSNumber numberWithInt:(50+(d%10)*100)];
            agingBucketAmount.bucketFive = [NSNumber numberWithInt:(500+(d%10)*100)];
            agingBucketAmount.bucketSix = [NSNumber numberWithInt:(750+(d%10)*100)];
            agingBucketAmount.bucketSeven = [NSNumber numberWithInt:(200+(d%10)*100)];
            
                        
            [_delinquentAmounts addObject:agingBucketAmount];
        }
        
        /*
        _delinquentAmounts = [[NSArray alloc] initWithObjects:
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:0],
                              [NSNumber numberWithInt:0], 
                              [NSNumber numberWithInt:0], 
                              [NSNumber numberWithInt:0], 
                              [NSNumber numberWithInt:0], 
                              [NSNumber numberWithInt:0], 
                              [NSNumber numberWithInt:0], 
                              nil ],
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:1000],
                              [NSNumber numberWithInt:1800], 
                              [NSNumber numberWithInt:3500], 
                              [NSNumber numberWithInt:1200], 
                              [NSNumber numberWithInt:2100], 
                              [NSNumber numberWithInt:2900], 
                              [NSNumber numberWithInt:4500], 
                              nil ],
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:2500],
                              [NSNumber numberWithInt:2000], 
                              [NSNumber numberWithInt:2100], 
                              [NSNumber numberWithInt:3200], 
                              [NSNumber numberWithInt:1800], 
                              [NSNumber numberWithInt:1000], 
                              [NSNumber numberWithInt:4000], 
                              nil ],  
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:1500],
                              [NSNumber numberWithInt:3000], 
                              [NSNumber numberWithInt:2200], 
                              [NSNumber numberWithInt:1200], 
                              [NSNumber numberWithInt:4300], 
                              [NSNumber numberWithInt:1100], 
                              [NSNumber numberWithInt:1900], 
                              nil ], 
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:1400],
                              [NSNumber numberWithInt:4000], 
                              [NSNumber numberWithInt:4600], 
                              [NSNumber numberWithInt:1500], 
                              [NSNumber numberWithInt:2500], 
                              [NSNumber numberWithInt:2200], 
                              [NSNumber numberWithInt:1600], 
                              nil ], 
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:2100],
                              [NSNumber numberWithInt:4800], 
                              [NSNumber numberWithInt:3300], 
                              [NSNumber numberWithInt:3700], 
                              [NSNumber numberWithInt:2300], 
                              [NSNumber numberWithInt:3200], 
                              [NSNumber numberWithInt:1400], 
                              nil ], 
                             [[NSArray alloc] initWithObjects:
                              [NSNumber numberWithInt:1000],
                              [NSNumber numberWithInt:5500], 
                              [NSNumber numberWithInt:1400], 
                              [NSNumber numberWithInt:4500], 
                              [NSNumber numberWithInt:3500], 
                              [NSNumber numberWithInt:3300], 
                              [NSNumber numberWithInt:2200], 
                              nil ], 
                             nil];
        */
        
        _agingBucketLabels= [[NSArray alloc] initWithObjects:@"", @"Current",@"1-30", @"31-60", @"61-90", @"91+", nil];
        
        
    }
    return self;
}

-(NSUInteger)getNumberOfDaysThisYearTillToday
{
    NSDateFormatter *df = [[NSDateFormatter alloc]init];  
    [df setDateFormat:@"MM-dd-yyyy"];  
    
    NSDate * newYearDay = [df dateFromString:@"01-01-2012"]; 
    NSDate * today = [[NSDate alloc]init];
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    
    /* //get number of months and days in this current months since 01-01-2012
     NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
     NSDateComponents *components = [gregorian components:unitFlags
     fromDate:newYearDay
     toDate:today options:0];
     NSInteger months = [components month];
     NSInteger days = [components day];
     */
    
    //to get number of days since 01-01-2012
    NSDateComponents *components = [gregorian components:NSDayCalendarUnit
                                                fromDate:newYearDay
                                                  toDate:today options:0];
    
    NSInteger days = [components day];
    
    return days;
    //NSLog(@"days = %d", days);
    
}


@end
